import { getDeviceDetailInfo, updateMetadata, updateProtocolStandby } from '@/api/iot/device'

const device = {
  state: {
    // 设备id
    deviceId: undefined,
    // 设备详情信息
    deviceDetailInfo: {
      state: {},
      deviceType: {},
      configuration: {},
      tags: []
    },
    // 详情页显示状态
    detailedPageVisible: false,
    // 当前活跃的标签
    tabsActiveName: 'deviceDetails',
    // 设备在线状态变化
    deviceOnlineChange: {},
    // 属性添加修改组件
    propertiesAddEdit: undefined,
    // 功能添加修改组件
    functionsAddEdit: undefined,
    // 事件添加修改组件
    eventsAddEdit: undefined,
    // 标签添加修改组件
    tagsAddEdit: undefined,
    // 协议支持组件
    protocolStandby: undefined,
  },

  mutations: {
    // 设备id
    DEVICE_ID: (state, data) => {
      state.deviceId = data
    },
    // 设备详情信息
    DEVICE_DETAIL_INFO: (state, data) => {
      state.deviceDetailInfo = { ...state.deviceDetailInfo, ...data }
    },
    // 详情页显示状态
    DEVICE_DETAILED_PAGE_VISIBLE: (state, data) => {
      state.detailedPageVisible = data
    },
    // 当前活跃的标签
    TABS_ACTIVE_NAME: (state, data) => {
      state.tabsActiveName = data
    },

    // 设备在线状态变化
    DEVICE_ONLINE_CHANGE: (state, data) => {
      state.deviceOnlineChange = data
    },

    // 属性添加修改组件
    DEVICE_PROPERTIES_ADD_EDIT: (state, data) => {
      state.propertiesAddEdit = data
    },
    // 功能添加修改组件
    DEVICE_FUNCTIONS_ADD_EDIT: (state, data) => {
      state.functionsAddEdit = data
    },
    // 事件添加修改组件
    DEVICE_EVENTS_ADD_EDIT: (state, data) => {
      state.eventsAddEdit = data
    },
    // 标签添加修改组件
    DEVICE_TAGS_ADD_EDIT: (state, data) => {
      state.tagsAddEdit = data
    },
    // 协议支持组件
    DEVICE_PROTOCOL_STANDBY: (state, data) => {
      state.protocolStandby = data
    },
  },

  actions: {
    // 获取设备详情
    GetDeviceDetailInfo({ commit }, deviceId) {
      return new Promise((resolve, reject) => {
        getDeviceDetailInfo(deviceId).then(({ data }) => {
          const deviceDetail = data || {}
          commit('DEVICE_DETAIL_INFO', deviceDetail)
          resolve(deviceDetail)
        }).catch(error => {
          reject(error)
        })
      })
    },

    /** 编辑属性操作 */
    EditDeviceProperties({ state }, id) {
      const properties = (JSON.parse(state.deviceDetailInfo.metadata) || {}).properties || []
      let rows = JSON.parse(JSON.stringify(properties.filter(item => id === item.id)))
      state.propertiesAddEdit.handleUpdate(rows[0])
    },

    /** 编辑功能操作 */
    EditDeviceFunctions({ state }, id) {
      const functions = (JSON.parse(state.deviceDetailInfo.metadata) || {}).functions || []
      let rows = JSON.parse(JSON.stringify(functions.filter(item => id === item.id)))
      state.functionsAddEdit.handleUpdate(rows[0])
    },

    /** 编辑事件操作 */
    EditDeviceEvents({ state }, id) {
      const events = (JSON.parse(state.deviceDetailInfo.metadata) || {}).events || []
      let rows = JSON.parse(JSON.stringify(events.filter(item => id === item.id)))
      state.eventsAddEdit.handleUpdate(rows[0])
    },

    /** 编辑事件操作 */
    EditDeviceTags({ state }, id) {
      const events = (JSON.parse(state.deviceDetailInfo.metadata) || {}).tags || []
      let rows = JSON.parse(JSON.stringify(events.filter(item => id === item.id)))
      state.tagsAddEdit.handleUpdate(rows[0])
    },

    /** 删除物模型数据 */
    DeleteDeviceMetadata({ commit, state }, { type, id }) {

      // 删除属性
      let metadata = JSON.parse(state.deviceDetailInfo.metadata)
      let byTypeMetadata = metadata[type]
      metadata[type] = byTypeMetadata.filter(item => id !== item.id)
      metadata = JSON.stringify(metadata)

      // 更新数据
      updateMetadata(state.deviceId, metadata).then(() => {
        // 更新缓存数据
        commit("DEVICE_DETAIL_INFO", { metadata: metadata, independentMetadata: true })
      })
    },

    /** 删除协议支持 */
    DeleteDeviceProtocolStandby({ commit, state }, id) {
      // 删除协议支持
      let protocolStandby = state.deviceDetailInfo.protocolStandby
      if (!protocolStandby) {
        return
      }

      protocolStandby = JSON.parse(protocolStandby)

      let protocolStandbySingle = protocolStandby[id]

      if (protocolStandbySingle) {
        delete protocolStandby[id]
      }
      protocolStandby = JSON.stringify(protocolStandby)

      // 更新协议支持
      updateProtocolStandby(state.deviceId, protocolStandby).then(() => {
        commit('DEVICE_DETAIL_INFO', { protocolStandby: protocolStandby })
      })

    },

    /** 查看协议支持 */
    ViewDeviceProtocolStandby({ state }, { id, thingType }) {
      let protocolStandby = state.deviceDetailInfo.protocolStandby

      let jsonObject = {
        id: ''
      }

      if (protocolStandby) {
        protocolStandby = JSON.parse(protocolStandby)
        jsonObject = protocolStandby[id] || jsonObject
      }

      jsonObject.thingType = thingType
      state.protocolStandby.handleCheck(id, jsonObject)
    },
  }
};

export default device
